package cn.com.cifi.mars.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import cn.com.cifi.mars.constant.Constants;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.com.cifi.mars.bean.vo.supply.SupplyConfirmExcelVO;

public class SupplyConfirmTemplate {
    
        private static final String TEMPLATE_PATH = "/template/supply_confirm_template.xls";
        
        private static final String CURRENT_PATTERN = "{current}";

	    private HSSFWorkbook workbook;

	    private HSSFSheet sheet;

	    /**
	     * 默认sheet索引
	     */
	    private int defaultSheetIndex = 0;

	    /**
	     * 数据开始行
	     */
	    private int dataBeginRow = 3;
	    
	    /**
	     * 数据模板行
	     */
	    private HSSFRow baseRow;

	    /**
	     * 当前行标
	     */
	    private int currentDataRowCursor = dataBeginRow;
	    
	    public SupplyConfirmTemplate(String deadline) {
	        try {
	            InputStream tempInputStream = SupplyConfirmTemplate.class.getResourceAsStream(TEMPLATE_PATH);
	            if (tempInputStream != null) {
	                workbook = new HSSFWorkbook(tempInputStream);
	                sheet = workbook.getSheetAt(defaultSheetIndex);
	                setSheetDate(deadline);
	                setCurrentDate(LocalDateTime.now());
	                baseRow = sheet.getRow(dataBeginRow);
	            }
	        } catch (Exception e) {
	            throw new RuntimeException(e);
	        }
	    }
	    
		public void setSheetDate(String deadline) {
	        workbook.setSheetName(defaultSheetIndex, workbook.getSheetName(defaultSheetIndex).replace("供货确认", "供货确认表".concat(deadline.replace("-", "")).concat("-").concat(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHH")))));
		}
		
		public void setCurrentDate(LocalDateTime date) {
			HSSFRow row = sheet.getRow(0);
	        if (row != null) {
	            for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
	            	HSSFCell cell = row.getCell(i);
	                if (cell != null && cell.getStringCellValue().contains(CURRENT_PATTERN)) {
	                    cell.setCellValue(cell.getStringCellValue().replace(CURRENT_PATTERN, String.valueOf(date.getYear())));
	                }
	            }
	        }
	    }

	    /**
	     * 批量插入行
	     *
	     * @param list
	     */
	    public synchronized void appendRows(List<SupplyConfirmExcelVO> list) {
	    	for (SupplyConfirmExcelVO vo : list) {
	    		this.appendRow(vo);
	        }
	    }
	    /**
	     * 插入行
	     *
	     * @param map
	     */
	    public synchronized void appendRow(SupplyConfirmExcelVO vo) {
	    	HSSFRow row = sheet.getRow(currentDataRowCursor);
	        if (row == null) {
	            row = sheet.createRow(currentDataRowCursor);
	        }
	        copyRowStyle(baseRow, row);
	        row.setHeightInPoints(Constants.HEIGHT_POINTS);
	        
	        AtomicInteger index = new AtomicInteger(-1);
	        //区域
	        this.appendCell(row, index.addAndGet(1),vo.getAreaName());
	        //城市公司
	        this.appendCell(row, index.addAndGet(1),vo.getCityName());
	        //项目信息
	        this.appendCell(row, index.addAndGet(1),vo.getProjectName());
	        this.appendCell(row, index.addAndGet(1),vo.getProjectCode());
	        //分期信息
	        this.appendCell(row, index.addAndGet(1),vo.getStageName());
	        this.appendCell(row, index.addAndGet(1),vo.getVersionName());
	        this.appendCell(row, index.addAndGet(1),vo.getConfirmStatus());
	        this.appendCell(row, index.addAndGet(1),vo.getConfirmUser());
	        this.appendCell(row, index.addAndGet(1),vo.getConfirmTime());
	        
	        currentDataRowCursor++;
	    }
	    
	    public void copyRowStyle(HSSFRow from, HSSFRow to) {
	        if (from != null && to != null && from != to && from.getRowStyle() != null) {
	            to.setRowStyle(from.getRowStyle());
	            to.setHeight(from.getHeight());
	        }
	    }
	    
	    public void copyCellStyle(HSSFCell from, HSSFCell to) {
	        if (from != null && to != null && from != to && from.getCellStyle() != null) {
	            to.setCellStyle(from.getCellStyle());
	        }
	    }

	    /**
	     * 插入列
	     *
	     * @param row
	     * @param index
	     * @param value
	     */
	    public synchronized void appendCell(HSSFRow row, int index, String value) {
	    	HSSFCell cell = row.getCell(index);
	        if (cell == null) {
	            cell = row.createCell(index);
	        }
	        copyCellStyle(baseRow.getCell(index), cell);
//	        cell.setCellValue(value == null ? "" : value);
	        ReportUtils.setCellValue(cell,value);
	    }

	    /**
	     * 导出
	     *
	     * @param outputStream
	     * @throws IOException
	     */
	    public void write(OutputStream outputStream) throws IOException {
	        workbook.write(outputStream);
	    }
}
